package com.lanyou.esb.web;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyou.cook.common.Constants;
import com.lanyou.esb.service.SocketLogService;
import com.lanyou.esb.web.param.SocketLogParam;
import com.lanyou.esb.web.serviceManagement.vo.SocketLogVo;

@RestController
@RequestMapping(Constants.BASE_URL_API + "/socketLogs")
public class SocketLogController {

	@Autowired
	private SocketLogService socketLogService;
	
	private static FastDateFormat fastDateFormat1 = FastDateFormat
			.getInstance(Constants.DATETIME_PATTERN_1);
	
	@RequestMapping("/search/findByExample")
	/*@RequiresPermissions(value = { "fieldSystemManagement:view","fieldSystemManagement:maintain"},logical=Logical.OR)*/
	public Page<SocketLogVo> findByExample(SocketLogParam param, Pageable pageable) {
		
		BigInteger total = (BigInteger) socketLogService.countAllSocketInterfaceLogs(param, pageable).get(0);
		if (total == null || total.longValue() <= 0) {
			return new PageImpl<SocketLogVo>(
					new ArrayList<SocketLogVo>(0), pageable, 0);
		}
		
		List<Object[]> interfaceDatas = socketLogService.findAllSocketInterfaceLogs(param, pageable);
		List<SocketLogVo> result = new ArrayList<SocketLogVo>(interfaceDatas.size());
		for (Object[] object : interfaceDatas) {
			SocketLogVo socketLogVo = new SocketLogVo();
			socketLogVo.setFieldName(object[0] != null ? (String) object[0] : "");
			socketLogVo.setSystemName(object[1] != null ? (String) object[1] : "");
			
			socketLogVo.setInterfaceName(object[2] != null ? (String) object[2] : "");
			socketLogVo.setSourceAddress(object[3] != null ? (String) object[3] : "");
			socketLogVo.setTargetAddress(object[4] != null ? (String) object[4] : "");
			socketLogVo.setFileName(object[5] != null ? (String) object[5] : "");
			socketLogVo.setFileSize((Integer) object[6]);
			
			socketLogVo.setExecuteTaskNode(object[7] != null ? (String) object[7] : "");
			
			socketLogVo.setTransportDate(fastDateFormat1.format((Date) object[8]));
			socketLogVo.setTransportResult(object[9] != null ? (String) object[9] : "");
			String transprotForExport = object[9] != null ? (String) object[9] : "";
			if(StringUtils.equals(transprotForExport, "0")){
				transprotForExport = "失败";
			}else if(StringUtils.equals(transprotForExport, "1")){
				transprotForExport = "成功";
			}else{
				transprotForExport = "";
			}
			socketLogVo.setTransprotForExport(transprotForExport);
			socketLogVo.setTransportResultDes(object[10] != null ? (String) object[10] : "");
			result.add(socketLogVo);
		}
		
		Page<SocketLogVo> page = new PageImpl<SocketLogVo>(result,
				pageable, total.longValue());
		
		return page;
	}
	
}
